﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Data.SqlClient;

namespace ERPForms
{
    public partial class ConturiBancareParteneriFrm : FrmRezumat
    {
        private Int64 _IDPartener = -1;
        public Int64 IDPartener
        {
            get { return _IDPartener; }
        }

        private String _ContBancar = String.Empty;
        public String ContBancar 
        {
            get { return _ContBancar; }
        }

        private Int16 _Banca = -1;
        public Int16 Banca
        {
            get { return _Banca; }
        }
        public ConturiBancareParteneriFrm(ModuleClass mc,Int64 idp,String cont, Int16 idb):
            base(mc)
        {
            _IDPartener = idp;
            _ContBancar = cont;
            _Banca = idb;
            InitializeComponent();
            workConnString = "ESRPConnectionString";
            ActiveControl = parteneriConturiBancareDGV;
            eSRPDataSet.ParteneriConturiBancare.IDPartenerColumn.DefaultValue = idp;
        }

        private void ConturiBancareParteneriFrm_Load(object sender, EventArgs e)
        {
            AddBindingSource(parteneriConturiBancareBindingSource);
            listTableAdaptor.Add(parteneriConturiBancareTableAdapter.Adapter);


            SqlConnection con = DeschideConexiune(workConnString);
            this.banciTableAdapter.Connection = con;
            this.banciTableAdapter.Fill(this.eSRPDataSet.Banci);
            this.parteneriConturiBancareTableAdapter.Connection = con;
            this.parteneriConturiBancareTableAdapter.FillByIDPartener(this.eSRPDataSet.ParteneriConturiBancare,IDPartener);
            InchideConexiune(con);
            HandleSalvare += Salvare;
            if (parteneriConturiBancareBindingSource.Count == 0 &&
                ContBancar!=String.Empty && Banca != -1)
            {
                ESRPDataSet.ParteneriConturiBancareRow rdPcb = ((DataRowView)parteneriConturiBancareBindingSource.AddNew()).Row
                    as ESRPDataSet.ParteneriConturiBancareRow;
                rdPcb.ContBancar = ContBancar;
                rdPcb.IDBanca = Banca;
                base.Salvare(this, null);
            }
            foreach (DataGridViewRow drv in parteneriConturiBancareDGV.Rows)
            {
                if (drv.Cells[ContBancarColumn.Index].Value.ToString() == ContBancar)
                {
                    drv.Cells[PrincipalColumn.Index].Value = true;
                    break;
                }
            }

        }

        private void parteneriConturiBancareDGV_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //daca nu sunt pe coloana cu unitate implicita
            if (PrincipalColumn.Index != e.ColumnIndex)
                return;
            foreach (DataGridViewRow dr in parteneriConturiBancareDGV.Rows)
            {
                dr.Cells[PrincipalColumn.Index].Value = false;
            }
            parteneriConturiBancareDGV[PrincipalColumn.Index, e.RowIndex].Value = true;
        }

        override public void Salvare(object sender, EventArgs args)
        {
            Boolean nuEste = true;
            foreach (DataGridViewRow dr in parteneriConturiBancareDGV.Rows)
            {
                if (Convert.ToBoolean(dr.Cells[PrincipalColumn.Index].Value) == true)
                {
                    nuEste = false;
                    break;
                }
            }
            if (nuEste)
            {
                MessageBox.Show("Nu a fost setat nici un cont principal!", "Stop Salvare", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }
            base.Salvare(this, args);
            if (EroareSalvare)
                return;
            foreach (DataGridViewRow dr in parteneriConturiBancareDGV.Rows)
            {
                if (Convert.ToBoolean(dr.Cells[PrincipalColumn.Index].Value) == true)
                {
                    SqlCommand cmd = new SqlCommand("UPDATE [Parteneri] SET [IDBanca]=" +
                        dr.Cells[BancaColumn.Index].Value.ToString() + ", [ContBancar]='" +
                         dr.Cells[ContBancarColumn.Index].Value.ToString() + "'  WHERE IDPartener=" + IDPartener.ToString(),
                         DeschideConexiune(workConnString));
                    cmd.ExecuteScalar();
                    InchideConexiune(cmd.Connection);
                    _ContBancar = dr.Cells[ContBancarColumn.Index].Value.ToString();
                    _Banca = Convert.ToInt16(dr.Cells[BancaColumn.Index].Value);
                    break;
                }
                    
            }
        }

        override public void Sterge(object sender, EventArgs args)
        {
            if (parteneriConturiBancareDGV.SelectedCells.Count == 0)
                return;
            Int32 rSel = parteneriConturiBancareDGV.SelectedCells[0].RowIndex;
            if (Convert.ToBoolean(parteneriConturiBancareDGV[PrincipalColumn.Index, rSel].Value) == true) 
            {
                MessageBox.Show("Contul principal nu poate fi sters!", "Stop Stergere", MessageBoxButtons.OK,MessageBoxIcon.Stop);
                return;
            }
            base.Sterge(sender, args);
        }

        private void parteneriConturiBancareDGV_CellValidated(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == ContBancarColumn.Index)
            {
                parteneriConturiBancareDGV[e.ColumnIndex, e.RowIndex].Value = parteneriConturiBancareDGV[e.ColumnIndex, e.RowIndex].Value.ToString().ToUpper();      
            }
        }

        
    }
}

/*<metadata name="banciBindingSource.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
    <value>643, 17</value>
  </metadata>
  <metadata name="PrincipalColumn.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
    <value>True</value>
  </metadata>
  <metadata name="parteneriConturiBancareBindingSource.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
    <value>380, 17</value>
  </metadata>
  <metadata name="banciBindingSource.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
    <value>643, 17</value>
  </metadata>
  <metadata name="eSRPDataSet.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
    <value>259, 17</value>
  </metadata>
  <metadata name="eSRPDataSet.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
    <value>259, 17</value>
  </metadata>
  <metadata name="parteneriConturiBancareTableAdapter.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
    <value>803, 17</value>
  </metadata>
  <metadata name="tableAdapterManager.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
    <value>17, 56</value>
  </metadata>
  <metadata name="banciTableAdapter.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
    <value>188, 56</value>
  </metadata>*/